package com.java110.mall.goods.dao.impl;

import com.alibaba.fastjson.JSONObject;
import com.java110.core.base.dao.BaseServiceDao;
import com.java110.mall.goods.dao.IMainCategoryProductServiceDao;
import com.java110.utils.constant.ResponseConstant;
import com.java110.utils.exception.DAOException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 主目录商品服务 与数据库交互
 * Created by wuxw on 2017/4/5.
 */
@Service("mainCategoryProductServiceDaoImpl")
//@Transactional
public class MainCategoryProductServiceDaoImpl extends BaseServiceDao implements IMainCategoryProductServiceDao {

    private static Logger logger = LoggerFactory.getLogger(MainCategoryProductServiceDaoImpl.class);


    /**
     * 保存主目录商品信息 到 instance
     *
     * @param info bId 信息
     * @throws DAOException DAO异常
     */
    @Override
    public void saveMainCategoryProductInfo(Map info) throws DAOException {
        logger.debug("保存主目录商品信息Instance 入参 info : {}", info);

        int saveFlag = sqlSessionTemplate.insert("mainCategoryProductServiceDaoImpl.saveMainCategoryProductInfo", info);

        if (saveFlag < 1) {
            throw new DAOException(ResponseConstant.RESULT_PARAM_ERROR, "保存主目录商品信息Instance数据失败：" + JSONObject.toJSONString(info));
        }
    }


    /**
     * 查询主目录商品信息（instance）
     *
     * @param info bId 信息
     * @return List<Map>
     * @throws DAOException DAO异常
     */
    @Override
    public List<Map> getMainCategoryProductInfo(Map info) throws DAOException {
        logger.debug("查询主目录商品信息 入参 info : {}", info);

        List<Map> infos = sqlSessionTemplate.selectList("mainCategoryProductServiceDaoImpl.getMainCategoryProductInfo", info);

        return infos;
    }


    /**
     * 修改主目录商品信息
     *
     * @param info 修改信息
     * @throws DAOException DAO异常
     */
    @Override
    public void updateMainCategoryProductInfo(Map info) throws DAOException {
        logger.debug("修改主目录商品信息Instance 入参 info : {}", info);

        int saveFlag = sqlSessionTemplate.update("mainCategoryProductServiceDaoImpl.updateMainCategoryProductInfo", info);

        if (saveFlag < 1) {
            throw new DAOException(ResponseConstant.RESULT_PARAM_ERROR, "修改主目录商品信息Instance数据失败：" + JSONObject.toJSONString(info));
        }
    }

    /**
     * 查询主目录商品数量
     *
     * @param info 主目录商品信息
     * @return 主目录商品数量
     */
    @Override
    public int queryMainCategoryProductsCount(Map info) {
        logger.debug("查询主目录商品数据 入参 info : {}", info);

        List<Map> infos
                = sqlSessionTemplate.selectList(
                "mainCategoryProductServiceDaoImpl.queryMainCategoryProductsCount", info);
        if (infos.size() < 1) {
            return 0;
        }

        return Integer.parseInt(infos.get(0).get("count").toString());
    }


    @Override
    public List<Map> queryMainCategoryAllGoods(Map info) {
        logger.debug("查询主目录商品信息 入参 info : {}", info);

        List<Map> infos = sqlSessionTemplate.selectList("mainCategoryProductServiceDaoImpl.queryMainCategoryAllGoods", info);

        return infos;
    }


    @Override
    public List<Map> queryMainCategoryAllServs(Map info) {
        logger.debug("查询服务信息 入参 info : {}", info);

        List<Map> infos = sqlSessionTemplate.selectList("mainCategoryProductServiceDaoImpl.queryMainCategoryAllServs", info);

        return infos;
    }


}
